package com.atguigu.mapreduce.wordcount;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

/**
 * @author weiranyi
 * @date 2021/10/25
 * @Description:
 *    输入key：文件偏移量 Long
 *    输入value：当前一行数据
 *    输出key：单词
 *    输出value：给单词打标记1
 */
public class WordCountMapper  extends Mapper<LongWritable, Text, Text, IntWritable> {
    private Text outK = new Text();
    private IntWritable outV = new IntWritable(1);
    /**
     *
     * @param key 文件偏移量 Long
     * @param value 当前一行数据
     * @param context 上下文对象，作用：程序执行之前产生的结果，以传递给后面的程序
     * @throws IOException
     * @throws InterruptedException
     */
    @Override
    protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, IntWritable>.Context context) throws IOException, InterruptedException {
        // 获取一行数据
        String line = value.toString();
        String[] words = line.split(" ");
        for (String word : words) {
            // 输出
            outK.set(word);
            context.write(outK, outV);
        }
    }
}
